package com.cma.cmabackend.controller;

import com.cma.cmabackend.entity.LabEmp;
import com.cma.cmabackend.entity.Parameter;
import com.cma.cmabackend.entity.Standard;
import com.cma.cmabackend.entity.in.InParameter;
import com.cma.cmabackend.entity.in.InParameterUnderStandard;
import com.cma.cmabackend.entity.in.InSubmitParameter;
import com.cma.cmabackend.entity.re.ReDetailParameter;
import com.cma.cmabackend.entity.re.ReParameter;
import com.cma.cmabackend.service.LabAdminService;
import com.cma.cmabackend.service.LabEmpService;
import com.cma.cmabackend.service.ParameterService;
import com.cma.cmabackend.service.StandardService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/parameter")
public class ParameterController {
    @Resource
    ParameterService parameterService;

    @Resource
    StandardService standardService;

    @Resource
    LabEmpService labEmpService;
    @Resource
    LabAdminService labAdminService;

    @PostMapping("")
    public Parameter add(String name, Integer standard_id, @RequestParam("emp") List<Integer> emps) {
        return parameterService.add(name, standard_id, emps);
    }

    @Transactional
    @PostMapping("cpus")
    public void createParameterUnderStandard(@RequestBody List<InParameterUnderStandard> data) {
        for (InParameterUnderStandard i : data) {
            Standard standard = standardService.findById(i.getId());
            for (InParameter j : i.getParameters()) {
                if (j.getId() == null) {
                    List<LabEmp> labEmpList = new ArrayList<>();
                    for (LabEmp l : j.getEmps()) {
                        labEmpList.add(labEmpService.findById(l.getId()));
                    }
                    j.setEmps(labEmpList);
                    j.setLabAdmin(labAdminService.findById(j.getLabAdmin().getId()));
                    j.setStatus(Parameter.Status.Uncompleted);
                    standard.getParameterList().add(j);
                }
            }
            standardService.save(standard);
        }
    }

    @Transactional
    @PostMapping("submit")
    public Parameter changeParameter(@RequestBody InSubmitParameter inSubmitParameter) {
        Standard standard = standardService.findById(inSubmitParameter.getStandardPara().getId());
        standard.setSample(inSubmitParameter.getStandardPara().getSample());
        standard.setSop(inSubmitParameter.getStandardPara().getSop());
        standard.setSimulation(inSubmitParameter.getStandardPara().getSimulation());
        standardService.save(standard);
        Parameter parameter = parameterService.findRealById(inSubmitParameter.getParameter().getId());
        parameter.setHumanList(inSubmitParameter.getParameter().getHumanList());
        parameter.setEquipmentList(inSubmitParameter.getParameter().getEquipmentList());
        parameter.setSample(inSubmitParameter.getParameter().getSample());
        parameter.setSop(inSubmitParameter.getParameter().getSop());
        parameter.setFacilityList(inSubmitParameter.getParameter().getFacilityList());
        parameter.setCompareValidList(inSubmitParameter.getParameter().getCompareValidList());
        parameter.setSimulation(inSubmitParameter.getParameter().getSimulation());
        return parameterService.save(parameter);
    }

    @GetMapping("change-status")
    public void changeStatus(Integer id) {
        parameterService.changeStatus(id);
    }

    @GetMapping("")
    public List<Parameter> getAll() {
        return parameterService.findAll();
    }

    @GetMapping("/getbystand")
    public List<Parameter> findByStand(Integer x) {
        return parameterService.findBystandardId(x);
    }

    @GetMapping("/getUncompletedbyemp")
    public List<ReDetailParameter> findByEmp(Integer id) {
        return parameterService.findByEmpIdAndStatus(id, Parameter.Status.Uncompleted);
    }

    @GetMapping("/getCompletedbyemp")
    public List<ReDetailParameter> findCompletedByEmp(Integer id) {
        return parameterService.findByEmpIdAndStatus(id, Parameter.Status.completed);
    }

    @GetMapping("/get")
    public ReParameter findById(Integer id) {
        return parameterService.findById(id);
    }

    @GetMapping("/sendback")
    public void sendBack(Integer id, Integer project) {
        parameterService.sendBack(id, project);
    }
}
